<?php 

class suipaiModel extends appModel
{
	
	public $card1 = "plHTpwpHlVbKDhEpymBYgvXXIGtA";  //88
	public $card2 = "plHTpwi90bTyJWTcZtJ92eFHxSIU";
	public $card3 = "plHTpwpTNycK95kt4LxpfRk-rEE0";
	public $ex_card = "plHTpwmlhQqJ_6itfucIAlIcADaI";
	
	
	public function ordering($val,$id)
	{
		$result = array('error' => 0, 'content' => '编辑成功');
		$sql = "UPDATE ".$this->table('picture')." SET praise='$val' WHERE id='".$id."'";
		$res = $this->query($sql);
		if(!res)
		{
			$result['error'] = 1;
			$result['content'] = '编辑失败';
		}
		return $result;
	}
	
	public function get_list($sort, $page, $limit = 10)
	{
		$where = "  where 1=1 ";
		if(!empty($sort['c']))
		{
			$dotime1 = strtotime($sort['c']);
			
			$where .=" AND created >=".$dotime1;
		}
		
		if(!empty($sort['k']))
		{
			$dotime2 = strtotime($sort['k']);
				
			$where .=" AND created <=".$dotime2;
		}
		
		$listPage = array('data' => '', 'pageLink' => '');
		
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('suipai')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.wxid ".
				$where." ORDER BY sp_id DESC";
		
		$result = $this->show_page($sql, $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		if($result['result'])
		foreach ($result['result'] as $k => $v)
		{
			$v['pic'] = str_replace('["',"",$v['pic']);
			$v['pic'] = str_replace('"]',"",$v['pic']);
			$listPage['data'][$k]['pic'] = explode('","',$v['pic']);
			
		}
		
		return $listPage;
		
	}
	
	
	public function get_pic_list($sort, $page, $limit = 10)
	{
		$where = "  where 1=1 ";
		if(!empty($sort['c']))
		{
			$dotime1 = strtotime($sort['c']);
				
			$where .=" AND created >=".$dotime1;
		}
	
		if(!empty($sort['k']))
		{
			$dotime2 = strtotime($sort['k']);
	
			$where .=" AND created <=".$dotime2;
		}
	
		$listPage = array('data' => '', 'pageLink' => '');
	
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('picture')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.wxid ".
				$where." ORDER BY praise desc ,created DESC";
	
		$result = $this->show_page($sql, $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		
		return $listPage;
	
	}
	
	
	public function get_code_list($page, $limit = 10)
	{
	    $where = "  where 1=1 ";
	    
	
	    $listPage = array('data' => '', 'pageLink' => '');
	
	    $sql = "SELECT  p1.* FROM ".$this->table('wx_pay_code')." as p1 ";
	        
	    $result = $this->getAll($sql);
	    
	    $sql = "SELECT count(*) as total , p1.codes FROM ".$this->table('wx_pay')." as p1 ".
	        "  GROUP BY codes ";
	        
	    $pay = $this->getAll($sql);
	    
	    
	    
	    foreach ($pay as $k => $v)
	    {
	        foreach ($result as $kk => $vv)
	        {
	            if($vv['codes'] == $v['codes'])
	            {
	                $result[$kk]['total'] = $v['total'];
	                break;
	            }
	            
	        }
	    }
	     
	   
	    
	    
	    $listPage['data'] = $result;
	
	    return $listPage;
	
	}
	
	
	function del_sp($id,$table)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
	
			$sql="DELETE FROM ".$this->table($table)." WHERE  sp_id='".$id."'";
	
			$res=$this->query($sql);
	
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
	
	
		}
		return $result;
	
	
	}
	
	function del_pic($id)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
	
			$sql="DELETE FROM ".$this->table("picture")." WHERE  id='".$id."'";
	
			$res=$this->query($sql);
	
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
	
	
		}
		return $result;
	
	
	}
	
	function del_code($id)
	{
	    $result=array('error'=>0,'content'=>'删除成功');
	    if($id)
	    {
	
	        $sql="DELETE FROM ".$this->table("wx_pay_code")." WHERE  id='".$id."'";
	
	        $res=$this->query($sql);
	
	        if(!$res)
	        {
	            $result['error']=1;
	            $result['content']='删除失败';
	        }
	
	
	    }
	    return $result;
	
	
	}
	
	function del_vote($id,$table)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
	
			$sql="DELETE FROM ".$this->table($table)." WHERE  id='".$id."'";
	
			$res=$this->query($sql);
	
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
	
	
		}
		return $result;
	
	
	}
	
	public function list_record($page=1,$cfg){
		$where=" ";
		$logic=" AND ";
		$time = date("YmdHis",time()-(120*60));
		foreach($cfg as $k=>$v)
		{
			if($k=="startTime"&&$v!=0){
				$startTime = date('YmdHis',strtotime($v));
				$where.=" p1.time_start>=".$startTime.$logic;
			}elseif ($k=="endTime"&&$v!=0){
				$endTime = date('YmdHis',strtotime($v));
				$where.=" p1.time_start<=".$endTime.$logic;
			}elseif($k=="out_trade_no"&&$v!=""){
				$where.=" p1.out_trade_no like '%".$v."%' ".$logic;
			}elseif($k=="transaction_id"&&$v!=""){
				$where.=" p1.transaction_id like '%".$v."%' ".$logic;
			}elseif($k=="nickname"&&$v!=""){
				$where.=" p4.nickname like '%".$v."%' ".$logic;
			}elseif($k=="pay_place"&&$v!=""){
				$where.=" p1.pay_place = '".$v."' ".$logic;
			}
			else{
				if($v!=0 && $k=="status"){
					switch ($v){
						case "1":$where.=" p1.pay_status = 'SUCCESS' ".$logic;break;
						case "2" :$where.=" p1.pay_status = 'NOTPAY' AND p1.time_start>$time ".$logic;break;
						case "3" :$where.=" p1.pay_status!='SUCCESS' AND p1.time_start<=$time ".$logic;break;
					}
				}
				
				
			}
		}
		$listPage=array('data'=>'','pageLink'=>'');
		$where1 = " $where 1=1 order by  p1.time_start desc ";
		$table2 = " ( SELECT p2.wxid,p2.nickname,p2.openid,p3.name,p3.phone FROM ".$this->table("wxusers")." p2 LEFT JOIN ".$this->table("wx_pay_user")." p3 ON p2.wxid=p3.wxid ) p4";
// 		$result=$this->get_page2(" p1.* ,  p2.nickname,p2.openid ,p3.name , p3.phone ",$this->table('wx_pay')." as p1 , ".$this->table('wxusers')."  as p2 , ".$this->table('wx_pay_user')."  as p3 ",$where1,$page,'30');
		$result=$this->get_page2(" p1.* ,  p4.nickname,p4.openid ,p4.name , p4.phone ",$this->table('wx_pay')."  p1 LEFT JOIN ".$table2." ON p1.wxid=p4.wxid",$where1,$page,'30');
		
		foreach($result as $k=>$v){
			$created = strtotime($v['time_start']);
			$result[$k]['time_start'] = date("Y-m-d H:i:s",$created);
			
			switch ($v['pay_place']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过
// 				case 1:$result[$k]['pay_place']="摇一摇电视";break;
			}
			
			if($v['pay_status']=="SUCCESS"){
				$result[$k]['pay_status_show'] = "支付成功";
			}else{
				if($v['time_start']>$time){
					$result[$k]['pay_status_show'] = "未付款";
				}else{
					$result[$k]['pay_status_show'] = "订单失效";
				}
			}
			
			$result[$k]['total_fee'] = $v['total_fee']/100;
		}
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		return $listPage;
	}
	
	public function get_order($id){
		$sql = "SELECT * FROM ".$this->table('wx_pay')." p1 left join ".$this->table("wxusers")." as p2 ON p1.wxid = p2.wxid WHERE p1.id=$id LIMIT 1";
		$order = $this->getRow($sql);
		return $order;
		
	}
	
	
	// 	新增记录
	public function insertRecord($info,$table)
	{
	    $data = yf_set_insert($info);
	    $sql = "insert into ".$this->table($table)." (".$data['key'].") values (".$data['val'].")";
	    $this->query($sql);
	    return $this->insert_id();
	}
	
	// 	新增记录
	public function checkCode($info)
	{
	    
	    $sql = " SELECT count(*) FROM ".$this->table("wx_pay_code")." WHERE codes = '".$info['codes']."'";
	    return $this->getOne($sql);
	    
	}
}

?>